//
//  RTJReceivableController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2019/3/26.
//  Copyright © 2019年 leewei. All rights reserved.
//

#import "RTJReceivableController.h"
#import "RTJBidRecordsDetailsCell.h"

@interface RTJReceivableController ()


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *data;
@property(nonatomic, assign) NSInteger currentPage;

@end

@implementation RTJReceivableController

-(NSMutableArray *)data {
    if (!_data) {
        _data = [NSMutableArray array];
    }
    
    return _data;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"回款详情";
    
    self.tableView.tableFooterView=[UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    self.tableView.estimatedRowHeight=300;
    
    self.currentPage = 1;
    self.tableView.mj_header = [CustomRefreshGifHeader headerWithRefreshingBlock:^{
        
        self.currentPage = 1;
        [self loadData];
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
}

-(void)loadData{
    
    [SVProgressHUD  showWithStatus:nil];
    
    if (self.data.count == 0) {
        
        [self.view showEmptyViewWithStr:@"" image:nil callback:nil];
        
    }
    
    [RTJNetWork asyncRequestWithURL:@"user/deal_load_repay" params:@{@"id":_binId} method:GET zxCompletion:^(id data, NSInteger status, BOOL success, NSString *errorMsg) {
        
        [SVProgressHUD dismiss];
        [self.view removeEmptyView];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        
        if (success) {
            
            if ([data[@"status"]integerValue] == 1) {
                
                NSArray *arr = [NSArray array];
                arr = data[@"data"][@"user_load_ids"];
                
                if (arr.count > 0) {
                    
                    [self.data removeAllObjects];
                    
                    [self.data addObjectsFromArray:arr];
                    
                    [self.tableView reloadData];
                    
                }else{
                    
                    [self.view showEmptyViewWithStr:@"暂无相关数据" image:[UIImage imageNamed:@"kbylogo"] callback:^{
                        [self loadData];
                    }];
                    
                }
                
            }else{
                
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",data[@"msg"]]];
                
                [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                    [self loadData];
                }];
                
            }
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",errorMsg]];
            
            [self.view showEmptyViewWithStr:errorMsg image:[UIImage imageNamed:@"kbylogo"] callback:^{
                [self loadData];
            }];
            
        }
        
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return  self.data.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewAutomaticDimension;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"RTJBidRecordsDetailsCell";
    
    RTJBidRecordsDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (!cell) {
        
        cell = [[NSBundle mainBundle] loadNibNamed:@"RTJBidRecordsDetailsCell" owner:self options:nil][1];
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.huikuanData = _data[indexPath.row];
    return  cell;
    
}


@end
